Advanced Lane Finding Project

The goals / steps of this project are the following:

  • Compute the camera calibration matrix and distortion coefficients given a set of chessboard images.
  • Apply a distortion correction to raw images.
  • Use color transforms, gradients, etc., to create a thresholded binary image.
  • Apply a perspective transform to rectify binary image ("birds-eye view").
  • Detect lane pixels and fit to find the lane boundary.
  • Determine the curvature of the lane and vehicle position with respect to center.
  • Warp the detected lane boundaries back onto the original image.
  • Output visual display of the lane boundaries and numerical estimation of lane curvature and vehicle position.

1 Import packages

In [1]:
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
%matplotlib inline

2 Calculate Camera Calibration Matrix and Distortion Coefficients

In [2]:
def find_object_image_sets(path_str, nx, ny, show_img=False):
    object_sets = []
    image_sets = []
    
    # Generate a matrics, have nx * ny rows, 3 colomns, type is np.float32
    object_points = np.zeros((nx * ny, 3), np.float32)
    object_points[:, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2)
    
    # Load all chessboard images path
    chessboard_imgs_path = glob.glob(path_str)

    for chessboard_img_path in chessboard_imgs_path:
        # Load image
        img = cv2.imread(chessboard_img_path)
        
        # Convert into grayscale
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        
        ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)
        
        if ret == True:
            object_sets.append(object_points)
            image_sets.append(corners)
            
            # Draw the corners on the chessboard
            if show_img == True:
                cv2.drawChessboardCorners(img, (nx, ny), corners, ret)
                f, ax = plt.subplots(1, 1, figsize=(7, 7))
                ax.imshow(img)
                ax.axis('off')
                ax.set_title(chessboard_img_path, fontsize = 20)
                
    size = (mpimg.imread(chessboard_imgs_path[0]).shape[1], mpimg.imread(chessboard_imgs_path[0]).shape[0])

    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(object_sets, image_sets, size, None, None)
    return (mtx, dist)

2.1 Show the chessboard with the corners in it

In [3]:
chessboard_imgs_path = "./camera_cal/*.jpg"

nx = 9 # The number of inside corners in x
ny = 6 # The number of inside corners in y

calibration_matrix, distortion_coefficients = find_object_image_sets(chessboard_imgs_path, nx, ny, True)

3 Undistort Image Using Calibration Matrix and Distortion Coefficient

In [4]:
def undistort_image(image, calibration_matrix, distortion_coefficients):
    undistorted_image = cv2.undistort(image, calibration_matrix, distortion_coefficients, None, calibration_matrix)
    return undistorted_image

3.1 Show the undistorted chessboard

In [5]:
# Load all chessboard images
chessboard_imgs_list = glob.glob(chessboard_imgs_path)

for chessboard_img in chessboard_imgs_list:
    original_image = cv2.imread(chessboard_img)
    undistorted_image = undistort_image(original_image, calibration_matrix, distortion_coefficients)
    fig, axes = plt.subplots(1, 2, figsize=(20,10), subplot_kw={'xticks':[], 'yticks':[]})
    axes[0].imshow(original_image)
    axes[0].set_title('Image before calibration', fontsize = 20)
    axes[1].imshow(undistorted_image)
    axes[1].set_title('Image after calibration', fontsize = 20)

3.2 Show the undistorted test road images

In [6]:
# Load all test images

road_imgs_path = "test_images/*.jpg"
road_imgs_list = glob.glob(road_imgs_path)

original_images = []
undistorted_images = []

for road_img in road_imgs_list:
    original_image = cv2.imread(road_img)
    original_images.append(original_image)
    
    undistorted_image = undistort_image(original_image, calibration_matrix, distortion_coefficients)
    undistorted_images.append(undistorted_image)
    
    fig, axes = plt.subplots(1, 2, figsize=(20,10), subplot_kw={'xticks':[], 'yticks':[]})
    
    axes[0].imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
    axes[0].set_title('Image before calibration', fontsize = 20)
    
    axes[1].imshow(cv2.cvtColor(undistorted_image, cv2.COLOR_BGR2RGB))
    axes[1].set_title('Image after calibration', fontsize = 20)

4 Edge Thresholding using Sobel

4.1 Sober Operation

In [7]:
# The function that takes an image, gradient orientation, and threshold min / max values.
def abs_sobel_thresh(img, orient='x', thresh=(0,255)):
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Apply x or y gradient with the OpenCV Sobel() function
    # and take the absolute value
    if orient == 'x':
        abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 1, 0))
    if orient == 'y':
        abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 0, 1))
    # Rescale back to 8 bit integer
    scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
    # Create a copy and apply the threshold
    binary_output = np.zeros_like(scaled_sobel)
    # Here I'm using inclusive (>=, <=) thresholds, but exclusive is ok too
    binary_output[(scaled_sobel >= thresh[0]) & (scaled_sobel <= thresh[1])] = 1

    # Return the result
    return binary_output

4.2 Magnitude of Gradient Thresholding

In [8]:
# The function to return the magnitude of the gradient for a given sobel kernel size and threshold values
def mag_thresh(img, sobel_kernel=3, mag_thresh=(0, 255)):
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Take both Sobel x and y gradients
    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    # Calculate the gradient magnitude
    gradmag = np.sqrt(sobelx**2 + sobely**2)
    # Rescale to 8 bit
    scale_factor = np.max(gradmag)/255 
    gradmag = (gradmag/scale_factor).astype(np.uint8) 
    # Create a binary image of ones where threshold is met, zeros otherwise
    binary_output = np.zeros_like(gradmag)
    binary_output[(gradmag >= mag_thresh[0]) & (gradmag <= mag_thresh[1])] = 1

    # Return the binary image
    return binary_output

4.3 Direction of Gradient Thresholding

In [9]:
# The function to threshold an image for a given range and Sobel kernel
def dir_threshold(img, sobel_kernel=3, thresh=(0, np.pi/2)):
    # Grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Calculate the x and y gradients
    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    # Take the absolute value of the gradient direction, 
    # apply a threshold, and create a binary image result
    absgraddir = np.arctan2(np.absolute(sobely), np.absolute(sobelx))
    binary_output =  np.zeros_like(absgraddir)
    binary_output[(absgraddir >= thresh[0]) & (absgraddir <= thresh[1])] = 1

    # Return the binary image
    return binary_output

4.4 Combination of Sober/Magnitude/Direction Gradient Thresholding

In [10]:
def gradient_threshold(img, ksize=15, sthresh=(20, 100), mthresh =(20, 100), dthresh=(0.7, 1.3)):
    gradx = abs_sobel_thresh(img, orient='x', thresh=sthresh)
    grady = abs_sobel_thresh(img, orient='y', thresh=sthresh)
    mag_binary = mag_thresh(img, sobel_kernel=ksize, mag_thresh=mthresh)
    dir_binary = dir_threshold(img, sobel_kernel=ksize, thresh=dthresh)
    
    combined_binary = np.zeros_like(dir_binary)
    combined_binary[((gradx == 1) & (grady == 1)) | ((mag_binary == 1) & (dir_binary == 1))] = 1
    
    return combined_binary

4.5 Show the test images after apply gradient thresholding

In [11]:
after_gradient_thresholding_imgs = []

for i in np.arange(0, len(original_images)):
    after_gradient_thresholding = gradient_threshold(undistorted_images[i])
    after_gradient_thresholding_imgs.append(after_gradient_thresholding)
    
    fig, axes = plt.subplots(1, 3, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
    
    axes[0].imshow(cv2.cvtColor(original_images[i], cv2.COLOR_BGR2RGB))
    axes[0].set_title('Original Image', fontsize = 20)
    axes[1].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
    axes[1].set_title('Image after calibration', fontsize = 20)
    axes[2].imshow(after_gradient_thresholding_imgs[i], cmap='gray')
    axes[2].set_title('Image after apply gradient thresholding', fontsize = 20)

4.6 Color Thresholding with S channel of HLS

In [12]:
# The function that thresholds the S-channel of HLS
def s_channel_HLS(img, thresh=(0, 255)):
    hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
    s_channel = hls[:,:,2]
    binary_output = np.zeros_like(s_channel)
    binary_output[(s_channel > thresh[0]) & (s_channel <= thresh[1])] = 1
    return binary_output

4.7 Color Thresholding with L channel of LUV

In [13]:
# The function that thresholds the L-channel of LUV
def l_channel_LUV(img, thresh=(0, 255)):
    luv = cv2.cvtColor(img, cv2.COLOR_BGR2LUV)
    l_channel = luv[:,:,0]
    binary_output = np.zeros_like(l_channel)
    binary_output[(l_channel > thresh[0]) & (l_channel <= thresh[1])] = 1
    return binary_output

4.8 Color Thresholding with L & B channel of LAB

In [14]:
# The function that thresholds the L & B channel of LAB
def lb_channel_LAB(img, lthresh=(0, 255), bthresh=(0,255)):
    lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    l_channel = lab[:,:,0]
    b_channel = lab[:,:,2]
    l_binary_output = np.zeros_like(l_channel)
    l_binary_output[(l_channel > lthresh[0]) & (l_channel <= lthresh[1])] = 1
    b_binary_output = np.zeros_like(b_channel)
    b_binary_output[(b_channel > bthresh[0]) & (b_channel <= bthresh[1])] = 1
    combined_binary = np.zeros_like(b_channel)
    combined_binary[(l_binary_output == 1) | (b_binary_output == 1)] = 1
    return combined_binary

4.9 Combination of Color Thresholding

In [15]:
def color_thresholding(img):
    # I finally didn't use HLS, since it has more noise
    luv_binary = l_channel_LUV(img, thresh=(210, 255))
    lab_binary = lb_channel_LAB(img, lthresh=(230, 255), bthresh=(155,255))
    combined_binary = np.zeros_like(lab_binary)
    combined_binary[(luv_binary == 1) | (lab_binary == 1)] = 1
    return combined_binary
In [16]:
after_color_thresholding_imgs = []

for i in np.arange(0, len(original_images)):
    after_color_thresholding = color_thresholding(undistorted_images[i])
    after_color_thresholding_imgs.append(after_color_thresholding)
    
    fig, axes = plt.subplots(1, 4, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
    
    axes[0].imshow(cv2.cvtColor(original_images[i], cv2.COLOR_BGR2RGB))
    axes[0].set_title('Original Image', fontsize = 20)
    
    axes[1].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
    axes[1].set_title('Image after calibration', fontsize = 20)
    
    axes[2].imshow(after_gradient_thresholding_imgs[i], cmap='gray')
    axes[2].set_title('Image after apply gradient thresholding', fontsize = 20)
    
    axes[3].imshow(after_color_thresholding_imgs[i], cmap='gray')
    axes[3].set_title('Image after apply color thresholding', fontsize = 20)

4.10 Define Mask Area

In [17]:
mask = np.zeros_like(after_gradient_thresholding_imgs[0])
img_tmp = np.copy(original_images[0])
height = 720
length = 1280
left_down = (230, height - 25)
left_top = (580, 445)
right_top = (680, 445)
right_down = (length - 110, height - 25)
trapezoid_outter = np.array([[left_down, left_top, right_top, right_down]])
cv2.fillPoly(mask, trapezoid_outter, 255)
result = cv2.polylines(img_tmp, [trapezoid_outter], True, (0,255,255), 3)
plt.imshow(result)
Out[17]:
<matplotlib.image.AxesImage at 0x7f076a9eb9b0>
In [18]:
mask = np.zeros_like(after_gradient_thresholding_imgs[0])
img_tmp = np.copy(original_images[0])
height = 720
length = 1280
left_down = (350, height - 25)
left_top = (580, 480)
right_top = (700, 480)
right_down = (length - 220, height - 25)
trapezoid_inner = np.array([[left_down, left_top, right_top, right_down]])
cv2.fillPoly(mask, trapezoid_inner, 255)
result = cv2.polylines(img_tmp, [trapezoid_inner], True, (0,255,255), 3)
plt.imshow(result)
Out[18]:
<matplotlib.image.AxesImage at 0x7f076a4f7940>
In [19]:
def region_of_interest(img, vertices_out, vertices_in):
    """
    Applies an image mask.
    
    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    `vertices` should be a numpy array of integer points.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)   
            
    #filling pixels inside the polygon defined by "vertices" with the fill color    
    cv2.fillPoly(mask, vertices_out, 1)
    
    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    
    cv2.fillPoly(masked_image, vertices_in, 0)
    return masked_image
In [20]:
masked_edges = region_of_interest(after_gradient_thresholding_imgs[0], trapezoid_outter, trapezoid_inner)
plt.imshow(masked_edges, cmap = 'gray')
Out[20]:
<matplotlib.image.AxesImage at 0x7f0764b8b358>

4.11 Combine Color and Gradient Thresholding with Mask

In [21]:
def thresholding_with_mask(img, trapezoid_out, trapezoid_in):
    color_thresh = color_thresholding(img)
    gradient_thresh = gradient_threshold(img)
    
    combined_binary = np.zeros_like(gradient_thresh)
    combined_binary[(color_thresh == 1) | (gradient_thresh == 1)] = 1
    
    after_mask = region_of_interest(combined_binary, trapezoid_out, trapezoid_in)
    return after_mask
In [22]:
after_thresholding_imgs = []

for i in np.arange(0, len(original_images)):
    after_thresholding = thresholding_with_mask(undistorted_images[i], trapezoid_outter, trapezoid_inner)
    after_thresholding_imgs.append(after_thresholding)
    
    fig, axes = plt.subplots(1, 3, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
    
    axes[0].imshow(cv2.cvtColor(original_images[i], cv2.COLOR_BGR2RGB))
    axes[0].set_title('Original Image', fontsize = 20)
    axes[1].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
    axes[1].set_title('Image after calibration', fontsize = 20)
    axes[2].imshow(after_thresholding_imgs[i], cmap='gray')
    axes[2].set_title('Image after apply thresholding', fontsize = 20)

5 Perspective Transformation

5. 1 Determine perspective transformation src and dst coordinates

In [23]:
mask = np.zeros_like(after_gradient_thresholding_imgs[0])
img_tmp = np.copy(original_images[2])
height = 720
length = 1280
left_down = (210,height)
left_top = (595,450)
right_top = (690,450)
right_down = (1110, height)
original_area = np.array([[left_down, left_top, right_top, right_down]])
cv2.fillPoly(mask, original_area, 255)
result = cv2.polylines(img_tmp, [original_area], True, (0,255,255), 3)
plt.figure(figsize = (16,8))
plt.imshow(result)
Out[23]:
<matplotlib.image.AxesImage at 0x7f076a5e49b0>

5.2 Calculate M and Minv for the perpective transformation

In [24]:
def calculate_M_Minv():    
    height = 720
    length = 1280
    
    # Four source coordinates
    src = np.float32([
        [210,height],
        [595,450],
        [690,450], 
        [1110, height]
    ])
    
    # Four desired coordinates
    dst = np.float32([
        [200, height], 
        [200, 0], 
        [1000, 0], 
        [1000, height]
    ])
    
    # Compute the perspective transform matrix, M
    M = cv2.getPerspectiveTransform(src, dst)
    Minv = cv2.getPerspectiveTransform(dst, src)
    return M, Minv
In [25]:
M, Minv = calculate_M_Minv()

5.3 Apply the perspective tranformation

In [26]:
def warp(img, M):
    img_size = (img.shape[1], img.shape[0])
    warped = cv2.warpPerspective(img, M, img_size)
    return warped

5.4 Show the whole test image transformation percedure

In [27]:
after_perspective_trans_imgs = []
after_perspective_trans_imgs_color = []

for i in np.arange(0, len(original_images)):
    after_perspective_trans = warp(after_thresholding_imgs[i], M)
    after_perspective_trans_imgs.append(after_perspective_trans)
    
    after_perspective_trans_color = warp(undistorted_images[i], M)
    after_perspective_trans_imgs_color.append(after_perspective_trans_color)
    
    fig, axes = plt.subplots(1, 5, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
    
    axes[0].imshow(cv2.cvtColor(original_images[i], cv2.COLOR_BGR2RGB))
    axes[0].set_title('Original Image', fontsize = 20)
    
    axes[1].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
    axes[1].set_title('Image after calibration', fontsize = 20)
    
    axes[2].imshow(after_perspective_trans_imgs_color[i], cmap='gray')
    axes[2].set_title('(Color image perspective trans)', fontsize = 20)
    
    axes[3].imshow(after_thresholding_imgs[i], cmap='gray')
    axes[3].set_title('Image after thresholding', fontsize = 20)
    
    axes[4].imshow(after_perspective_trans_imgs[i], cmap='gray')
    axes[4].set_title('Image after perspective trans', fontsize = 20)

6 Finding the Lines

6.1 Histogram Peaks

In [28]:
def hist(img):
    # Grab only the bottom half of the image
    # Lane lines are likely to be mostly vertical nearest to the car
    # Sum across image pixels vertically - make sure to set an `axis`
    # i.e. the highest areas of vertical lines should be larger values
    histogram_t = np.sum(img[img.shape[0]//2:,:], axis=0)
    return histogram_t
In [29]:
histogram_imgs = []

for i in np.arange(0, len(original_images)):
    histogram = hist(after_perspective_trans_imgs[i])
    histogram_imgs.append(histogram)
    
    fig, axes = plt.subplots(1, 2, figsize=(35,10))
    
    axes[0].imshow(after_perspective_trans_imgs[i], cmap='gray')
    axes[0].set_title('Image after perspective trans', fontsize = 40)
    
    axes[1].plot(histogram_imgs[i])
    axes[1].set_title('Histo of perspective trans', fontsize = 40)

6.2 Sliding Window

In [30]:
def find_lane_pixels(binary_warped):
    # Take a histogram of the bottom half of the image
    histogram = np.sum(binary_warped[binary_warped.shape[0]//2:,:], axis=0)
    
    # Create an output image to draw on and visualize the result
    out_img = np.dstack((binary_warped, binary_warped, binary_warped))
    
    # Find the peak of the left and right halves of the histogram
    # These will be the starting point for the left and right lines
    midpoint = np.int(histogram.shape[0]//2)
    leftx_base = np.argmax(histogram[:midpoint])
    rightx_base = np.argmax(histogram[midpoint:]) + midpoint

    # HYPERPARAMETERS
    # Choose the number of sliding windows
    nwindows = 9
    # Set the width of the windows +/- margin
    margin = 100
    # Set minimum number of pixels found to recenter window
    minpix = 50

    # Set height of windows - based on nwindows above and image shape
    window_height = np.int(binary_warped.shape[0]//nwindows)
    
    # Identify the x and y positions of all nonzero pixels in the image
    nonzero = binary_warped.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])
    
    # Current positions to be updated later for each window in nwindows
    leftx_current = leftx_base
    rightx_current = rightx_base

    # Create empty lists to receive left and right lane pixel indices
    left_lane_inds = []
    right_lane_inds = []

    # Step through the windows one by one
    for window in range(nwindows):
        # Identify window boundaries in x and y (and right and left)
        win_y_low = binary_warped.shape[0] - (window+1)*window_height
        win_y_high = binary_warped.shape[0] - window*window_height
        win_xleft_low = leftx_current - margin
        win_xleft_high = leftx_current + margin
        win_xright_low = rightx_current - margin
        win_xright_high = rightx_current + margin
        
        # Draw the windows on the visualization image
        cv2.rectangle(out_img,
                      (win_xleft_low, win_y_low),
                      (win_xleft_high, win_y_high),
                      (0, 255,0), 
                      8) 
        cv2.rectangle(out_img,
                      (win_xright_low, win_y_low),
                      (win_xright_high, win_y_high),
                      (0, 255, 0), 
                      8) 
        
        # Identify the nonzero pixels in x and y within the window #
        good_left_inds = ((nonzeroy >= win_y_low) & 
                          (nonzeroy < win_y_high) & 
                          (nonzerox >= win_xleft_low) &  
                          (nonzerox < win_xleft_high)).nonzero()[0]
        
        good_right_inds = ((nonzeroy >= win_y_low) & 
                           (nonzeroy < win_y_high) & 
                           (nonzerox >= win_xright_low) &  
                           (nonzerox < win_xright_high)).nonzero()[0]
        
        # Append these indices to the lists
        left_lane_inds.append(good_left_inds)
        right_lane_inds.append(good_right_inds)
        
        # If you found > minpix pixels, recenter next window on their mean position
        if len(good_left_inds) > minpix:
            leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
        
        if len(good_right_inds) > minpix:        
            rightx_current = np.int(np.mean(nonzerox[good_right_inds]))

    left_lane_inds = np.concatenate(left_lane_inds)
    right_lane_inds = np.concatenate(right_lane_inds)

    # Extract left and right line pixel positions
    leftx = nonzerox[left_lane_inds]
    lefty = nonzeroy[left_lane_inds] 
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds]

    return leftx, lefty, rightx, righty, out_img


def fit_polynomial(binary_warped):
    # Find our lane pixels first
    leftx, lefty, rightx, righty, out_img = find_lane_pixels(binary_warped)

    # Fit a second order polynomial to each using `np.polyfit`
    left_fit = np.polyfit(lefty, leftx, 2)
    right_fit = np.polyfit(righty, rightx, 2)

    # Generate x and y values for plotting
    ploty = np.linspace(0, binary_warped.shape[0]-1, binary_warped.shape[0] )
    left_fitx = left_fit[0]*ploty**2 + left_fit[1]*ploty + left_fit[2]
    right_fitx = right_fit[0]*ploty**2 + right_fit[1]*ploty + right_fit[2]

    ## Visualization ##
    # Colors in the left and right lane regions
    out_img[lefty, leftx] = [255, 0, 0]
    out_img[righty, rightx] = [255, 0, 0]

    return out_img, left_fitx, right_fitx, ploty, leftx, lefty, rightx, righty
In [31]:
plt.figure(figsize = (16,8))
plt.imshow(after_perspective_trans_imgs[0], cmap='gray')
Out[31]:
<matplotlib.image.AxesImage at 0x7f075cd4bb70>
In [32]:
plt.figure(figsize = (16,8))
plt.imshow(fit_polynomial(after_perspective_trans_imgs[0].astype(np.uint8))[0]) # TODO need to transform to uint8
Out[32]:
<matplotlib.image.AxesImage at 0x7f075cd104e0>
In [33]:
after_find_line_imgs = []

for i in np.arange(0, len(original_images)):
    after_find_line, _, _, _, _, _, _, _ = fit_polynomial(after_perspective_trans_imgs[i].astype(np.uint8))
    
    after_find_line_imgs.append(after_find_line)
    
    fig, axes = plt.subplots(1, 4, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
    
    axes[0].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
    axes[0].set_title('Image after calibration', fontsize = 20)
    
    axes[1].imshow(after_perspective_trans_imgs_color[i])
    axes[1].set_title('(Color image perspective trans)', fontsize = 20)
        
    axes[2].imshow(after_perspective_trans_imgs[i], cmap='gray')
    axes[2].set_title('Image after perspective trans', fontsize = 20)
    
    axes[3].imshow(after_find_line_imgs[i])
    axes[3].set_title('Image after find lane', fontsize = 20)

6.3 Draw lane line and unwarp back to original perspective

In [34]:
def draw_lines(undist, warped, left_fitx, right_fitx, ploty, Minv):
    
    # Create an image to draw the lines on
    warp_zero = np.zeros_like(warped).astype(np.uint8)
    color_warp = np.dstack((warp_zero, warp_zero, warp_zero))

    # Recast the x and y points into usable format for cv2.fillPoly()
    pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))])
    pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, ploty])))])
    pts = np.hstack((pts_left, pts_right))

    # Draw the lane onto the warped blank image
    cv2.fillPoly(color_warp, np.int_([pts]), (255, 102, 0))

    # Warp the blank back to original image space using inverse perspective matrix (Minv)
    newwarp = cv2.warpPerspective(color_warp, Minv, (undist.shape[1], undist.shape[0])) 
    # Combine the result with the original image
    result = cv2.addWeighted(undist, 1, newwarp, 0.7, 0)
    
    return result
In [35]:
plt.figure(figsize = (16,8))
after_find_line, left_fitx, right_fitx, ploty, _, _, _, _ = fit_polynomial(after_perspective_trans_imgs[0].astype(np.uint8))
t3 = draw_lines(undistorted_images[0], after_perspective_trans_imgs[0], left_fitx, right_fitx, ploty, Minv)
plt.imshow(cv2.cvtColor(t3, cv2.COLOR_BGR2RGB))
Out[35]:
<matplotlib.image.AxesImage at 0x7f076a542278>
In [36]:
result_img = []

for i in np.arange(0, len(original_images)):
    after_find_line, left_fitx, right_fitx, ploty, _, _, _, _ = fit_polynomial(after_perspective_trans_imgs[i].astype(np.uint8))
    result = draw_lines(undistorted_images[0], after_perspective_trans_imgs[0], left_fitx, right_fitx, ploty, Minv)
    result_img.append(result)
    
    fig, axes = plt.subplots(1, 4, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
    
    axes[0].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
    axes[0].set_title('Image after calibration', fontsize = 20)
    
    axes[1].imshow(after_perspective_trans_imgs_color[i])
    axes[1].set_title('(Color image perspective trans)', fontsize = 20)
    
    axes[2].imshow(after_find_line_imgs[i])
    axes[2].set_title('Image after find lane', fontsize = 20)
    
    axes[3].imshow(cv2.cvtColor(result_img[i], cv2.COLOR_BGR2RGB))
    axes[3].set_title('Final result', fontsize = 20)

7 Calculate radius and offset

In [37]:
def measure_curvature_real(leftx, lefty, rightx, righty):
    '''
    Calculates the curvature of polynomial functions in meters.
    '''
    # Define conversions in x and y from pixels space to meters
    ym_per_pix = 30/720 # meters per pixel in y dimension
    xm_per_pix = 3.7/700 # meters per pixel in x dimension
    
    left_fit_cr = np.polyfit(lefty * ym_per_pix, leftx * xm_per_pix, 2)
    right_fit_cr = np.polyfit(righty * ym_per_pix, rightx * xm_per_pix, 2)
    
    # Define y-value where we want radius of curvature
    # We'll choose the maximum y-value, corresponding to the bottom of the image
    y_eval = 720
    
    # Calculation of R_curve (radius of curvature)
    left_curverad = ((1 + (2 * left_fit_cr[0] * y_eval * ym_per_pix + left_fit_cr[1])**2)**1.5) / np.absolute(2 * left_fit_cr[0])
    right_curverad = ((1 + (2 * right_fit_cr[0] * y_eval * ym_per_pix + right_fit_cr[1])**2)**1.5) / np.absolute(2 * right_fit_cr[0])
    radius = (left_curverad + right_curverad) / 2
    offset = (640 - (leftx[-1] + rightx[-1]) / 2) * xm_per_pix
    
    return radius, offset
In [38]:
# Calculate the radius of curvature in meters for both lane lines

_, _, _, _, leftx, lefty, rightx, righty = fit_polynomial(after_perspective_trans_imgs[0].astype(np.uint8))
radius, offset = measure_curvature_real(leftx, lefty, rightx, righty)

print(radius, 'm', offset, 'm')
297.25586211962 m 0.7664285714285715 m
In [39]:
def add_text(img, radius, offset):
    cv2.putText(img, 
                'Radius of curvature of the lane: {}(m)'.format(round(radius, 3)), 
                (500, 20 * 3),
                fontFace = 16, 
                fontScale = 1, 
                color=(255, 255, 255), 
                thickness = 2) 
    cv2.putText(img, 
                'Offset from center: {}(m)'.format(round(offset, 3)), 
                (500, 20 * 5),
                fontFace = 16, 
                fontScale = 1, 
                color=(255, 255, 255), 
                thickness = 2) 
    return img

8 The Final Pipeline

In [40]:
def process_image(image1):
    image2 = undistort_image(image1, calibration_matrix, distortion_coefficients)
    image3 = thresholding_with_mask(image2, trapezoid_outter, trapezoid_inner)
    image4 = after_perspective_trans = warp(image3, M)
    image41 = warp(image2, M)
    image5, left_fitx, right_fitx, ploty, leftx, lefty, rightx, righty = fit_polynomial(image4.astype(np.uint8))
    radius, offset = measure_curvature_real(leftx, lefty, rightx, righty)
    image6 = draw_lines(image2, image4, left_fitx, right_fitx, ploty, Minv)
    small_1 = cv2.resize(image5, (int(image5.shape[1] / 4), int(image5.shape[0] / 4)))
    x_offset_1 = 30
    y_offset_1 = 30
    result = add_text(image6, radius, offset)
    result[y_offset_1 : y_offset_1 + small_1.shape[0], x_offset_1 : x_offset_1 + small_1.shape[1]] = small_1
    small_2 = cv2.resize(image41, (int(image41.shape[1] / 4), int(image41.shape[0] / 4)))
    x_offset_2 = 30
    y_offset_2 = 60 + small_1.shape[0]
    result[y_offset_2 : y_offset_2 + small_2.shape[0], x_offset_2 : x_offset_2 + small_2.shape[1]] = small_2
    #result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
    return result
In [41]:
result = process_image(cv2.imread('test_images/test1.jpg'))
plt.figure(figsize = (16,8))
plt.imshow(result)
Out[41]:
<matplotlib.image.AxesImage at 0x7f076a62be10>

9 Generate Video

In [42]:
from moviepy.editor import VideoFileClip
from IPython.display import HTML

# Using VideoFileClip('project_video.mp4').subclip(0,5) to generate a 5 seconds subclip for test
output = 'project_video_output.mp4'
clip = VideoFileClip('project_video.mp4')
output_clip = clip.fl_image(process_image)
%time output_clip.write_videofile(output, audio=False)
[MoviePy] >>>> Building video project_video_output.mp4
[MoviePy] Writing video project_video_output.mp4
100%|█████████▉| 1260/1261 [06:35<00:00,  3.19it/s]
[MoviePy] Done.
[MoviePy] >>>> Video ready: project_video_output.mp4 

CPU times: user 20min 21s, sys: 4.66 s, total: 20min 26s
Wall time: 6min 35s
In [43]:
HTML("""
<video width="640" height="360" controls>
  <source src="{0}">
</video>
""".format(output))
Out[43]: